<?php

require('./../../passbook/Database/connect.php');

$valor    = $_REQUEST['codigo'];
$tipo     = substr($valor, 0, 1);
$num_pass = substr($valor, 1, strlen($valor) - 1);
$hoy      = date("Y") . '-' . date("m") . '-' . date('d');

switch ($tipo) {
    case 'e':
        
        $insert_log = "insert into accesos_evento (num_pass, fecha) values ($num_pass, '$hoy')";
        mysqli_query($connection, $insert_log);
        
        $query_datos = "select c.nombre, c.a_paterno, c.a_materno, c.empresa, c.descripcion_empresa, e.nombre as nombre_evento,
                ep.estatus from cliente as c, passbook_evento as ep, evento as e where ep.num_pass = $num_pass and
                ep.num_cliente = c.num_cliente and e.num_evento = ep.num_evento";
        
        $result = mysqli_query($connection, $query_datos);
        $row    = mysqli_fetch_array($result, MYSQLI_ASSOC);
        
        $estatus = "No Utilizado";
        
        if ($row['estatus'] == 1) {
            $log_query  = "select count(fecha) as cuenta from accesos_evento where num_pass = $num_pass";
            $result_log = mysqli_query($connection, $log_query);
            $row_log    = mysqli_fetch_array($result_log, MYSQLI_ASSOC);
            
            $cuenta = $row_log['cuenta'];
            $plural = "vez.";
            
            if ($cuenta > 1) {
                $plural = "veces.";
            }
            
            $estatus = "Utilizado $cuenta $plural";
        }
        
        $update_passbook = "update passbook_evento set estatus = 1 where num_pass = $num_pass";
        mysqli_query($connection, $update_passbook);
        
        $response = array(
            "nombre"              => $row['nombre'].' '.$row['a_paterno'].' '.$row['a_materno'],
            "empresa"             => $row['empresa'],
            "descripcion_empresa" => $row['descripcion_empresa'], 
            "nombre_evento"       => $row['nombre_evento'],
            "estatus"             => $estatus
        );
        
        break;
    
    case 'm':
        
        $insert_log = "insert into accesos_membresia (num_pass, fecha) values ($num_pass, '$hoy')";
        mysqli_query($connection, $insert_log);
        
        $query_datos = "select c.nombre, c.a_paterno, c.a_materno, c.empresa, c.descripcion_empresa, 
            tm.tipo, pm.f_adquisicion, pm.f_vencimiento, pm.estatus from cliente as c, tipo_membresia as tm,
            passbook_membresia as pm where pm.num_pass = $num_pass and c.num_cliente = pm.num_cliente and tm.id = pm.tipo";
        
        $result = mysqli_query($connection, $query_datos);
        $row    = mysqli_fetch_array($result, MYSQLI_ASSOC);
        
        $vencimiento   = $row['f_vencimiento'];
        $cuenta        = 0;
        $estatus       = "Válida";
        
        if (strtotime($vencimiento) < time()) {
            $update_passbook = "update passbook_membresia set estatus = 0";
            mysqli_query($connection, $update_passbook);
            
            $estatus    = "Vencida";
                        
            $select_log = "select count(fecha) as cuenta from accesos_membresia where fecha > $vencimiento and num_pass = $num_pass";
            $result_log = mysqli_query($connection, $select_log);
            $row_log    = mysqli_fetch_array($result_log, MYSQLI_ASSOC);
            
            $cuenta = $row_log['cuenta'];
        }
        
        $response = array(
            "nombre"              => $row['nombre'].' '.$row['a_paterno'].' '.$row['a_materno'],
            "empresa"             => $row['empresa'],
            "descripcion_empresa" => $row['descripcion_empresa'],
            "tipo_membresia"      => "Membresía ".$row['tipo'],
            "miembro_desde"       => $row['f_adquisicion'],
            "fecha_vencimiento"   => $vencimiento,
            "estatus"             => $estatus,
            "accesos_vencidos"    => $cuenta
        );
        
        break;
    
    case'c':
        
        $insert_log = "insert into accesos_curso (num_pass, fecha) values ($num_pass, '$hoy')";
        mysqli_query($connection, $insert_log);
        
        $query_datos = "select c.nombre, c.a_paterno, c.a_materno, c.empresa, c.descripcion_empresa, cu.nombre as nombre_curso,
            pc.estatus from cliente as c, curso as cu, passbook_curso as pc where pc.num_pass = $num_pass and
            c.num_cliente = pc.num_cliente and cu.num_curso = pc.num_curso";
        
        $result = mysqli_query($connection, $query_datos);
        $row    = mysqli_fetch_array($result, MYSQLI_ASSOC);
        
        $estatus = "No Utilizado";
        
        if ($row['estatus'] == 1) {
            $log_query  = "select count(fecha) as cuenta from accesos_curso where num_pass = $num_pass";
            $result_log = mysqli_query($connection, $log_query);
            $row_log    = mysqli_fetch_array($result_log, MYSQLI_ASSOC);
            
            $cuenta = $row_log['cuenta'];
            $plural = "vez.";
            
            if ($cuenta > 1) {
                $plural = "veces.";
            }
            
            $estatus = "Utilizado $cuenta $plural";
        }
        
        $update_passbook = "update passbook_curso set estatus = 1 where num_pass = $num_pass";
        mysqli_query($connection, $update_passbook);
        
        $response = array(
            "nombre"              => $row['nombre'].' '.$row['a_paterno'].' '.$row['a_materno'],
            "empresa"             => $row['empresa'],
            "descripcion_empresa" => $row['descripcion_empresa'], 
            "nombre_evento"       => $row['nombre_curso'],
            "estatus"             => $estatus
        );
        
        break;
    
    default:
        $response = array();
        break;
    
        
}

header('Content-type: application/json');
echo json_encode($response);

?>